/*
 ============================================================================
 Name        : hev-status-icon.c
 Author      : Heiher <admin@heiher.info>
 Version     : 0.0.2
 Copyright   : Copyright (c) 2010 everyone.
 Description : Linkage 802.1x for Unix-like operating system.
 ============================================================================
 */

#include "hev-status-icon.h"
#include "hev-main-dialog.h"

/*#define HEV_STATUS_ICON_ONLINE		"."G_DIR_SEPARATOR_S"res"G_DIR_SEPARATOR_S"linkage-online.png"
#define HEV_STATUS_ICON_OFFLINE		"."G_DIR_SEPARATOR_S"res"G_DIR_SEPARATOR_S"linkage-offline.png"*/
#define HEV_STATUS_ICON_ONLINE		"/usr/share/icons/linkage-online.png"
#define HEV_STATUS_ICON_OFFLINE		"/usr/share/icons/linkage-offline.png"

static void hev_status_icon_real_activate(GtkStatusIcon * self);
static void hev_status_icon_real_popup_menu(GtkStatusIcon * self, guint button, guint time);
static void hev_status_icon_quit_menu_item_real_activate(GtkMenuItem * menu_item, gpointer data);

#define HEV_STATUS_ICON_GET_PRIVATE(obj)	(G_TYPE_INSTANCE_GET_PRIVATE((obj), HEV_TYPE_STATUS_ICON, HevStatusIconPrivate))

typedef struct _HevStatusIconPrivate HevStatusIconPrivate;

struct _HevStatusIconPrivate
{
	GtkWidget * main_dialog;
};

G_DEFINE_TYPE(HevStatusIcon, hev_status_icon, GTK_TYPE_STATUS_ICON);

static void hev_status_icon_dispose(GObject * obj)
{
	HevStatusIcon * self = HEV_STATUS_ICON(obj);
	HevStatusIconPrivate * priv = HEV_STATUS_ICON_GET_PRIVATE(self);

	/* main_dialog START */
	if(G_IS_OBJECT(priv->main_dialog))
	{
		g_object_unref(priv->main_dialog);

		priv->main_dialog = NULL;
	}
	/* main_dialog END */

	G_OBJECT_CLASS(hev_status_icon_parent_class)->dispose(obj);
}

static void hev_status_icon_finalize(GObject * obj)
{
	HevStatusIcon * self = HEV_STATUS_ICON(obj);
	HevStatusIconPrivate * priv = HEV_STATUS_ICON_GET_PRIVATE(self);

	G_OBJECT_CLASS(hev_status_icon_parent_class)->finalize(obj);
}

static void hev_status_icon_class_init(HevStatusIconClass * klass)
{
	GObjectClass * obj_class = G_OBJECT_CLASS(klass);

	obj_class->dispose = hev_status_icon_dispose;
	obj_class->finalize = hev_status_icon_finalize;

	GTK_STATUS_ICON_CLASS(klass)->activate = hev_status_icon_real_activate;
	GTK_STATUS_ICON_CLASS(klass)->popup_menu = hev_status_icon_real_popup_menu;

	g_type_class_add_private(klass, sizeof(HevStatusIconPrivate));
}

static void hev_status_icon_init(HevStatusIcon * self)
{
	HevStatusIconPrivate * priv = HEV_STATUS_ICON_GET_PRIVATE(self);

	gtk_status_icon_set_visible(GTK_STATUS_ICON(self), TRUE);
}

GtkStatusIcon * hev_status_icon_new(void)
{
	return g_object_new(HEV_TYPE_STATUS_ICON, NULL);
}

void hev_status_icon_set_main_dialog(HevStatusIcon * self, GtkWidget * main_dialog)
{
	HevStatusIconPrivate * priv = NULL;

	g_return_if_fail(HEV_IS_STATUS_ICON(self));
	g_return_if_fail(HEV_IS_MAIN_DIALOG(main_dialog));

	priv = HEV_STATUS_ICON_GET_PRIVATE(self);

	priv->main_dialog = g_object_ref(main_dialog);
}

void hev_status_icon_set_online(HevStatusIcon * self)
{
	g_return_if_fail(HEV_IS_STATUS_ICON(self));

	gtk_status_icon_set_from_file(GTK_STATUS_ICON(self),
			HEV_STATUS_ICON_ONLINE);
	gtk_status_icon_set_tooltip_text(GTK_STATUS_ICON(self), "己连接");
}

void hev_status_icon_set_offline(HevStatusIcon * self)
{
	g_return_if_fail(HEV_IS_STATUS_ICON(self));

	gtk_status_icon_set_from_file(GTK_STATUS_ICON(self),
			HEV_STATUS_ICON_OFFLINE);
	gtk_status_icon_set_tooltip_text(GTK_STATUS_ICON(self), "未连接");
}

static void hev_status_icon_real_activate(GtkStatusIcon * self)
{
	HevStatusIconPrivate * priv = HEV_STATUS_ICON_GET_PRIVATE(self);

	if(gtk_widget_get_visible(priv->main_dialog))
	{
		gtk_widget_hide(priv->main_dialog);
	}
	else
	{
		gtk_widget_show(priv->main_dialog);
	}
}

static void hev_status_icon_real_popup_menu(GtkStatusIcon * self, guint button, guint time)
{
	GtkWidget * menu = NULL;
	GtkWidget * image_menu_item = NULL;

	image_menu_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT, NULL);
	g_signal_connect(G_OBJECT(image_menu_item), "activate",
			G_CALLBACK(hev_status_icon_quit_menu_item_real_activate), NULL);

	menu = gtk_menu_new();
	gtk_menu_shell_append(GTK_MENU_SHELL(menu), image_menu_item);
	gtk_widget_show_all(menu);
	gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL,
			button, time);
}

static void hev_status_icon_quit_menu_item_real_activate(GtkMenuItem * menu_item, gpointer data)
{
	gtk_main_quit();
}
